home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / xampp-win32-1.6.5-installer.exe / htdocs / xampp / biorhythm.php < prev    next >
Encoding:
PHP Script  |  2007-12-20  |  6.2 KB  |  187 lines

  1. <?php
  2.     include "langsettings.php";
  3.  
  4.     // Biorhythm by Till Gerken
  5.     // http://www.zend.com/zend/tut/dynamic.php
  6.     //
  7.     // Multi-language support by Kai Oswald Seidler, 2003
  8.     //
  9.     // Print a standard page header
  10.     //
  11.     function pageHeader() {
  12.         global $TEXT;
  13.  
  14.         echo "<html><head>";
  15.         echo '<link href="xampp.css" rel="stylesheet" type="text/css">';
  16.         echo "<title></title>";
  17.         echo "</head><body>";
  18.  
  19.         echo " <p><h1>".$TEXT['bio-head']."</h1>";
  20.         echo $TEXT['bio-by']." Till Gerken<br><a class='black' href='http://www.zend.com/zend/tut/dynamic.php'>http://www.zend.com/zend/tut/dynamic.php</a><p>";
  21.     }
  22.  
  23.     //
  24.     // Print a standard page footer
  25.     //
  26.     function pageFooter() {
  27.         echo "</body></html>";
  28.     }
  29.  
  30.     //
  31.     // Function to draw a curve of the biorythm
  32.     // Parameters are the day number for which to draw,
  33.     // period of the specific curve and its color
  34.     //
  35.     function drawRhythm($daysAlive, $period, $color) {
  36.         global $daysToShow, $image, $diagramWidth, $diagramHeight;
  37.  
  38.         // get day on which to center
  39.         $centerDay = $daysAlive - ($daysToShow / 2);
  40.  
  41.         // calculate diagram parameters
  42.         $plotScale = ($diagramHeight - 25) / 2;
  43.         $plotCenter = ($diagramHeight - 25) / 2;
  44.  
  45.         // draw the curve
  46.         for ($x = 0; $x <= $daysToShow; $x++) {
  47.             // calculate phase of curve at this day, then Y value
  48.             // within diagram
  49.             $phase = (($centerDay + $x) % $period) / $period * 2 * pi();
  50.             $y = 1 - sin($phase) * (float)$plotScale + (float)$plotCenter;
  51.  
  52.             // draw line from last point to current point
  53.             if ($x > 0) {
  54.                 imageLine($image, $oldX, $oldY,    $x * $diagramWidth / $daysToShow, $y, $color);
  55.             }
  56.  
  57.             // save current X/Y coordinates as start point for next line
  58.             $oldX = $x * $diagramWidth / $daysToShow;
  59.             $oldY = $y;
  60.         }
  61.     }
  62.  
  63.     //
  64.     // ---- MAIN PROGRAM START ----
  65.     //
  66.  
  67.     // check if we already have a date to work with,
  68.     // if not display a form for the user to enter one
  69.     //
  70.     if (!isset($_REQUEST['birthdate']))    {
  71.         pageHeader();
  72. ?>
  73.         <form method="post" action="<?php echo basename($_SERVER['PHP_SELF']); ?>">
  74.             <!-- Please enter your birthday: -->
  75.             <?php echo $TEXT['bio-ask']; ?>:
  76.             <br>
  77.             <input type="text" name="birthdate" value="MM/DD/YYYY"><p>
  78.             <input type="submit" value="<?php echo $TEXT['bio-ok']; ?>">
  79.             <input type="hidden" name="showpng" value="1">
  80.         </form>
  81. <?php
  82.  
  83.         include("showcode.php");
  84.  
  85.         pageFooter();
  86.         exit;
  87.     }
  88.  
  89.     // get different parts of the date
  90.     $birthMonth = substr($_REQUEST['birthdate'], 0, 2);
  91.     $birthDay = substr($_REQUEST['birthdate'], 3, 2);
  92.     $birthYear = substr($_REQUEST['birthdate'], 6, 4);
  93.  
  94.     // check date for validity, display error message if invalid
  95.     if (!@checkDate($birthMonth, $birthDay, $birthYear)) {
  96.         pageHeader();
  97.  
  98.         //print("The date '$birthMonth/$birthDay/$birthYear' is invalid.");
  99.         echo "<h2>".$TEXT['bio-error1']." '$birthMonth/$birthDay/$birthYear' ".$TEXT['bio-error2'].".</h2>";
  100.  
  101.         pageFooter();
  102.         exit;
  103.     }
  104.  
  105.     if (isset($_POST['showpng']) && ($_POST['showpng'] == 1)) {
  106.         pageHeader();
  107.  
  108.         echo "<img src=".basename($_SERVER['PHP_SELF'])."?birthdate=".urlencode($_REQUEST['birthdate'])." alt=''>";
  109.  
  110.         pageFooter();
  111.         exit;
  112.     }
  113.  
  114.     // specify diagram parameters (these are global)
  115.     $diagramWidth = 710;
  116.     $diagramHeight = 400;
  117.     $daysToShow = 30;
  118.  
  119.     // calculate the number of days this person is alive
  120.     // this works because Julian dates specify an absolute number
  121.     // of days -> the difference between Julian birthday and
  122.     // "Julian today" gives the number of days alive
  123.     $daysGone = abs(gregorianToJD($birthMonth, $birthDay, $birthYear) - gregorianToJD(date("m"), date("d"), date("Y")));
  124.  
  125.     // create image
  126.     $image = imageCreate($diagramWidth, $diagramHeight);
  127.  
  128.     // allocate all required colors
  129.     $colorBackgr = imageColorAllocate($image, 192, 192, 192);
  130.     $colorForegr = imageColorAllocate($image, 255, 255, 255);
  131.     $colorGrid = imageColorAllocate($image, 0, 0, 0);
  132.     $colorCross = imageColorAllocate($image, 0, 0, 0);
  133.     $colorPhysical = imageColorAllocate($image, 0, 0, 255);
  134.     $colorEmotional = imageColorAllocate($image, 255, 0, 0);
  135.     $colorIntellectual = imageColorAllocate($image, 0, 255, 0);
  136.  
  137.     // clear the image with the background color
  138.     imageFilledRectangle($image, 0, 0, $diagramWidth - 1, $diagramHeight - 1, $colorBackgr);
  139.  
  140.     // calculate start date for diagram and start drawing
  141.     $nrSecondsPerDay = 60 * 60 * 24;
  142.     $diagramDate = time() - ($daysToShow / 2 * $nrSecondsPerDay) + $nrSecondsPerDay;
  143.  
  144.     for ($i = 1; $i < $daysToShow; $i++) {
  145.         $thisDate = getDate($diagramDate);
  146.         $xCoord = ($diagramWidth / $daysToShow) * $i;
  147.  
  148.         // draw day mark and day number
  149.         imageLine($image, $xCoord, $diagramHeight - 25, $xCoord, $diagramHeight - 20, $colorGrid);
  150.         imageString($image, 3, $xCoord - 5, $diagramHeight - 16, $thisDate["mday"], $colorGrid);
  151.  
  152.         $diagramDate += $nrSecondsPerDay;
  153.     }
  154.  
  155.     // draw rectangle around diagram (marks its boundaries)
  156.     imageRectangle($image, 0, 0, $diagramWidth - 1, $diagramHeight - 20, $colorGrid);
  157.  
  158.     // draw middle cross
  159.     imageLine($image, 0, ($diagramHeight - 20) / 2, $diagramWidth, ($diagramHeight - 20) / 2, $colorCross);
  160.     imageLine($image, $diagramWidth / 2, 0, $diagramWidth / 2, $diagramHeight - 20,    $colorCross);
  161.  
  162.     // print descriptive text into the diagram
  163.     imageString($image, 3, 10, 10, $TEXT['bio-birthday'].": $birthDay.$birthMonth.$birthYear", $colorCross);
  164.     imageString($image, 3, 10, 26, $TEXT['bio-today'].":    ".date("d.m.Y"), $colorCross);
  165.     imageString($image, 3, 10, $diagramHeight - 42, $TEXT['bio-physical'], $colorPhysical);
  166.     imageString($image, 3, 10, $diagramHeight - 58, $TEXT['bio-emotional'], $colorEmotional);
  167.     imageString($image, 3, 10, $diagramHeight - 74, $TEXT['bio-intellectual'], $colorIntellectual);
  168.  
  169.     // now draw each curve with its appropriate parameters
  170.     drawRhythm($daysGone, 23, $colorPhysical);
  171.     drawRhythm($daysGone, 28, $colorEmotional);
  172.     drawRhythm($daysGone, 33, $colorIntellectual);
  173.  
  174.     // set the content type
  175.     header("Content-Type: image/png");
  176.  
  177.     // create an interlaced image for better loading in the browser
  178.     imageInterlace($image, 1);
  179.  
  180.     // mark background color as being transparent
  181.     imageColorTransparent($image, $colorBackgr);
  182.  
  183.     // now send the picture to the client (this outputs all image data directly)
  184.     imagePNG($image);
  185.     exit;
  186. ?>
  187.